package com.starchen.optimalselection.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starchen.optimalselection.common.constant.ProductConstant;
import com.starchen.optimalselection.common.mapper.AttrAndGroupRelationMapper;
import com.starchen.optimalselection.common.model.AttrAndGroupRelation;
import com.starchen.optimalselection.product.service.AttrAndGroupRelationService;
import com.starchen.optimalselection.common.service.AttributeService;
import com.starchen.optimalselection.common.mapper.AttributeGroupMapper;
import com.starchen.optimalselection.common.model.Attribute;
import com.starchen.optimalselection.common.model.AttributeGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AttrAndGroupRelationServiceImpl extends ServiceImpl<AttrAndGroupRelationMapper, AttrAndGroupRelation> implements AttrAndGroupRelationService {

    @Autowired
    private AttributeService attrService;

    @Autowired
    private AttrAndGroupRelationMapper attrAttrgroupRelationMapper;

    @Autowired
    private AttributeGroupMapper attrGroupMapper;

    @Override
    public List<Attribute> getRelationByGroup(Long groupId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("attr_group_id",groupId);
        List<AttrAndGroupRelation> relations = baseMapper.selectList(queryWrapper);
        List<Attribute> attrList = new ArrayList<>();
        if(relations != null && relations.size() > 0){
            List<Long> attrIds = relations.stream().map(item -> item.getAttrId()).collect(Collectors.toList());
            //attrList = attrService.listByIds(attrIds);
        }
        return attrList;
    }

    @Override
    public void deleteRelation(AttrAndGroupRelation attrGroupRelationVo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("attr_id",attrGroupRelationVo.getAttrId());
        queryWrapper.eq("attr_group_id",attrGroupRelationVo.getAttrGroupId());
        //attrAttrgroupRelationMapper.delete(queryWrapper);
    }

    /**
     * 获取当前分组没有关联的所有属性
     * @param attrGroupId
     * @return
     */
    @Override
    public List<Attribute> getNoRelationAttr(Long attrGroupId) {
        //1、当前分组只能关联自己所属的分类里面的所有属性
        AttributeGroup attrGroupEntity = attrGroupMapper.selectById(attrGroupId);
        Long catelogId = attrGroupEntity.getCategoryId();
        //2、当前分组只能关联别的分组没有引用的属性
        //2.1)、当前分类下的其他分组
        List<AttributeGroup> group = attrGroupMapper.selectList(new QueryWrapper<AttributeGroup>().eq("category_id", catelogId));
        List<Long> groupIds = group.stream().map(item -> item.getId()).collect(Collectors.toList());

        //2.2)、这些分组关联的属性
        List<AttrAndGroupRelation> userGroupIds = attrAttrgroupRelationMapper.selectList(new QueryWrapper<AttrAndGroupRelation>().in("attr_group_id", groupIds));
        List<Long> attrIds = userGroupIds.stream().map(item -> item.getAttrId()).collect(Collectors.toList());

        //2.3)、从当前分类的所有属性中移除这些属性；
        QueryWrapper<Attribute> wrapper = new QueryWrapper<Attribute>().eq("category_id", catelogId).eq("attr_type", ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode());
        if(attrIds!=null && attrIds.size()>0){
            wrapper.notIn("id", attrIds);
        }

        List<Attribute> attrList = attrService.list(wrapper);

        return attrList;
    }

}